/**
 * 质量体系环境保护认证
 */
enterprsie_EnterpriseEuthentication_store = Ext.extend(Ext.data.GroupingStore,
		{
			constructor : function() {
				// this.cfg = _cfg;
				enterprsie_EnterpriseEuthentication_store.superclass.constructor
						.call(this, {
							proxy : new Ext.data.HttpProxy({
								url : 'cxxqysjsb/quantity_environment/getalldataEnterpriseEuthentication.action?uid='
										+ userId,// 用户的ID
								method : 'post'
							}),
							autoLoad : true,
							sortInfo : {
								field : 'authentication_id',
								direction : "ASC"
							},
							groupField : 'declare_year',
							reader : new Ext.data.JsonReader({
										fields : ['authentication_id',
												'editable',
												'authentication_type',
												'enterprise_id',
												'authentication_is_new',
												'authentication_type_sb',
												'authentication_check_pass',
												'authentication_time',
												'authentication_name',
												'authentication_department',
												'declare_year']
									})
						})
			}
		});
enterprsie_EnterpriseEuthentication_grid = Ext.extend(Ext.grid.GridPanel, {
	store : null,
	addwin : null,
	editwin : null,
	constructor : function() {
		this.store = new enterprsie_EnterpriseEuthentication_store();
		this.addwin = new addenterprsie_EnterpriseEuthentication_formpanel_win();
		enterprsie_EnterpriseEuthentication_grid.superclass.constructor.call(
				this, {
					loadMask : {
						msg : '数据加载中...'
					},
					title : '企业基本情况-质量体系环境保护认证',
					iconCls : 'SMLPoolManage_sml',
					sm : arrsm,
					frame : true,
					view : new Ext.grid.GroupingView({
						forceFit : true,
						groupTextTpl : '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "条数据" : "条数据"]})'
					}),
					cm : new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
							{
								header : 'ID',
								width : 10,
								dataIndex : 'authentication_id',
								id : 'authentication_id',
								hidden : true,
								align : 'center'
							}, {
								header : '企业编码',
								width : 10,
								dataIndex : 'enterprise_id',
								id : 'enterprise_id',
								hidden : true,
								align : 'center'
							}, {
								header : '申报年份',
								width : 100,
								dataIndex : 'declare_year',
								id : 'declare_year',
								sortable : false,
								renderer : this.getYearName,
								align : 'center'
							}, {
								header : '认证类型',
								width : 100,
								dataIndex : 'authentication_type',
								id : 'authentication_type',
								renderer : this.getauthentication_typeName,
								align : 'center'
							}, {
								header : '认证名称',
								width : 150,
								dataIndex : 'authentication_name',
								id : 'authentication_name',
								align : 'center'
							}, {
								header : '认证单位',
								width : 150,
								dataIndex : 'authentication_department',
								id : 'authentication_department',
								align : 'center'
							}, {
								header : '认证时间',
								width : 100,
								dataIndex : 'authentication_time',
								id : 'authentication_time',
								sortable : false,
								align : 'center'
							}, {
								header : '申报类型',
								width : 100,
								dataIndex : 'authentication_type_sb',
								id : 'authentication_type_sb',
								sortable : false,
								renderer : this.getcheckType,
								align : 'center'
							}, {
								header : '审核状态',
								width : 100,
								dataIndex : 'authentication_check_pass',
								id : 'authentication_check_pass',
								sortable : false,
								renderer : this.standard_check_passName,
								align : 'center'
							}]),
					tbar : [{
						text : '增加<font color="red">' + OPTION_NDDECLARE_YEAR
								+ '年' + getDataType(OPTION_DATA_TYPE)
								+ '</font>质量体系环境保护认证数据',
						scope : this,
						iconCls : 'itemadd',
						handler : function() {

							this.addwin.show(Ext.getDom('login-view'));
						}
					}, '-', {
						text : '修改',
						scope : this,
						tooltip : '只允许修改草稿数据',
						iconCls : 'smlEdit_edit',
						handler : function() {
							var ro = this.getSelectionModel().getSelected();
							if (!ro) {
								Ext.Msg.show({
											title : '提示',
											msg : '你没有选择数据',
											icon : Ext.Msg.WARNING,
											buttons : Ext.Msg.OK
										});
								return;
							} else {
								var checkresult = ro
										.get('authentication_check_pass');
								if (checkresult == 0) {
									Ext.Msg.show({
												title : '提示',
												msg : '正在审核的数据不能修改',
												icon : Ext.Msg.ERROR,
												buttons : Ext.Msg.OK
											});
									return;
								}
								if (checkresult == 1 || checkresult == 2) {
									Ext.Msg.show({
												title : '提示',
												msg : '审核完成的数据不能修改',
												icon : Ext.Msg.ERROR,
												buttons : Ext.Msg.OK
											});
									return;
								}
								var cfg = {
									eaid : ro.get('authentication_id'),
									eid : ro.get('enterprise_id'),
									declare_year : ro.get('declare_year'),
									authentication_type : ro
											.get('authentication_type'),
									authentication_name : ro
											.get('authentication_name'),
									authentication_department : ro
											.get('authentication_department'),
									authentication_time : ro
											.get('authentication_time'),
									authentication_type_sb : ro
											.get('authentication_type_sb')
								};
								this.editwin = new editenterprsie_EnterpriseEuthentication_formpanel_win(cfg);
								this.editwin.show(Ext.getDom('west-div'));
								this.editwin.on('editok',function(){this.getStore().reload();},this);
							}
						}
					}, '-', {
						text : '刷新',
						scope : this,
						iconCls : 'refreshtty',
						handler : function() {
							this.getStore().reload();
						}
					}]
				});
		this.addwin.on('addok', function() {
					this.getStore().reload();
				}, this);
	},
	getYearName : function(value) {
		return value + '年';
	},
	getauthentication_typeName : function(code) {
		return getquality_envirment_combox_store(code);
	},
	getcheckType : function(code) {
		return '<font color="red">' + getDataType(code) + '</font>';
	},
	standard_check_passName : function(code) {
		return getCheckResultName(code);
	}
});
/**
 * 增加新纪录的formpael
 * 
 * @class addenterprsie_EnterpriseEuthentication_formpanel
 * @extends Ext.form.FormPanel
 */
addenterprsie_EnterpriseEuthentication_formpanel = Ext.extend(
		Ext.form.FormPanel, {
			constructor : function() {
				addenterprsie_EnterpriseEuthentication_formpanel.superclass.constructor
						.call(this, {
									frame : true,
									height : 190,
									bodyStyle : 'padding: 10px 0px 0 10px;',
									labelWidth : 80,
									labelAlign : 'right',
									defaults : {
										anchor : '95%',
										allowBlank : true,
										msgTarget : 'side'
									},
									items : [{
												xtype : 'hidden',
												name : 'userid',
												value : userId
											}, {
												xtype : 'textfield',
												fieldLabel : '申报年度',
												anchor : '99%',
												name : 'type',
												readOnly : true,
												disabled : true,
												value : OPTION_NDDECLARE_YEAR
														+ '年'
														+ getDataType(OPTION_DATA_TYPE)
														+ ''
											}, {
												xtype : 'combo',
												fieldLabel : '认证类型',
												hiddenName : 'authentication_type',
												triggerAction : 'all',
												displayField : 'name',
												readOnly : true,
												valueField : 'id',
												mode : 'local',
												anchor : '99%',
												store : quality_envirment_combox_store,
												emptyText : '请选择认证类型',
												allowBlank : false,
												blankText : '请选择认证类型'
											}, {
												xtype : 'textfield',
												fieldLabel : '认证名称',
												name : 'authentication_name',
												allowBlank : false,
												blankText : '认证名称不能为空',
												anchor : '99%'
											}, {
												xtype : 'textfield',
												fieldLabel : '认证单位',
												name : 'authentication_department',
												allowBlank : false,
												blankText : '认证单位不能为空',
												anchor : '99%'
											}, {
												xtype : 'datefield',
												fieldLabel : '认证时间',
												name : 'authentication_time',
												allowBlank : false,
												blankText : '认证时间不能为空',
												anchor : '99%'
											}]
								})
			}
		});
addenterprsie_EnterpriseEuthentication_formpanel_win = Ext.extend(Ext.Window, {
	panel : null,
	constructor : function() {
		this.panel = new addenterprsie_EnterpriseEuthentication_formpanel();
		addenterprsie_EnterpriseEuthentication_formpanel_win.superclass.constructor
				.call(this, {
							width : 500,
							title : '申报<font color="red">'
									+ OPTION_NDDECLARE_YEAR + '年'
									+ getDataType(OPTION_DATA_TYPE)
									+ '</font>质量体系环境认证数据',
							iconCls : 'itemadd',
							// layout : 'form',
							closeAction : 'hide',
							frame : true,
							modal : true,
							plain : true,
							items : this.panel,
							buttons : [{
										text : '保存草稿',
										scope : this,
										iconCls : 'itemadd',
										handler : this.submit
									}, {
										text : '取消',
										scope : this,
										iconCls : 'cancel',
										handler : function() {
											this.panel.getForm().reset();
											this.hide()
										}
									}]
						});
		this.addEvents('addok');
	},
	submit : function() {
		var form = this.panel.getForm();
		if (!form.isValid()) {
			Ext.Msg.show({
						title : '提示',
						msg : '数据没有填写完整',
						icon : Ext.Msg.WARNING,
						buttons : Ext.MessageBox.OK
					});
			return;
		} else {
			/**
			 * 
			 */
			var uid = form.findField('userid').getValue();
			/**
			 * 
			 */
			var authentication_type = form.findField('authentication_type')
					.getValue();
			/**
			 * 
			 */
			var authentication_name = form.findField('authentication_name')
					.getValue();
			/**
			 * 
			 */
			var authentication_department = form
					.findField('authentication_department').getValue();
			/**
			 * 
			 */
			var authentication_time = form.findField('authentication_time')
					.getValue();
			var year = OPTION_NDDECLARE_YEAR;
			var type = OPTION_DATA_TYPE;
			Ext.Ajax.request({
				url : 'cxxqysjsb/quantity_environment/insertnewEnterpriseAuthentication.action',
				params : {
					uid : uid,
					year : year,
					type : type,
					authentication_type : authentication_type,
					authentication_name : authentication_name,
					authentication_department : authentication_department,
					authentication_time : authentication_time
				},
				scope : this,
				success : function(response, options) {
					if (response.responseText == 'ok') {
						this.fireEvent('addok');
						this.hide();
					} else if (response.responseText == 'fail') {
						Ext.Msg.show({
									title : '提示',
									msg : '数据传输失败',
									icon : Ext.Msg.WARNING,
									buttons : Ext.Msg.OK
								});
						return;
					}
				}
			});
		}
	}

});
editenterprsie_EnterpriseEuthentication_formpanel = Ext.extend(
		Ext.form.FormPanel, {
			cfg : null,
			constructor : function(_cfg) {
				this.cfg = _cfg;
				editenterprsie_EnterpriseEuthentication_formpanel.superclass.constructor
						.call(this, {
									frame : true,
									height : 150,
									bodyStyle : 'padding: 10px 0px 0 10px;',
									labelWidth : 80,
									labelAlign : 'right',
									defaults : {
										anchor : '95%',
										allowBlank : true,
										msgTarget : 'side'
									},
									items : [{
										xtype : 'hidden',
										name : 'eaid',
										value : this.cfg.eaid
											// 标准ID
										}, {
										xtype : 'hidden',
										name : 'eid',
										value : this.cfg.eid
											// 企业ID
										}, {
										xtype : 'combo',
										fieldLabel : '认证类型',
										hiddenName : 'authentication_type',
										triggerAction : 'all',
										displayField : 'name',
										value : this.cfg.authentication_type,
										readOnly : true,
										valueField : 'id',
										mode : 'local',
										anchor : '99%',
										store : quality_envirment_combox_store,
										emptyText : '请选择认证类型',
										allowBlank : false,
										blankText : '请选择认证类型'
									}, {
										xtype : 'textfield',
										fieldLabel : '认证名称',
										value : this.cfg.authentication_name,
										name : 'authentication_name',
										allowBlank : false,
										blankText : '认证名称不能为空',
										anchor : '99%'
									}, {
										xtype : 'textfield',
										fieldLabel : '认证单位',
										value : this.cfg.authentication_department,
										name : 'authentication_department',
										allowBlank : false,
										blankText : '认证单位不能为空',
										anchor : '99%'
									}, {
										xtype : 'datefield',
										fieldLabel : '认证时间',
										value : this.cfg.authentication_time,
										name : 'authentication_time',
										allowBlank : false,
										blankText : '认证时间不能为空',
										anchor : '99%'
									}]
								});
			}
		});
editenterprsie_EnterpriseEuthentication_formpanel_win = Ext.extend(Ext.Window,
		{
			panel : null,
			cfg : null,
			constructor : function(_cfg) {
				this.cfg = _cfg;
				this.panel = new editenterprsie_EnterpriseEuthentication_formpanel(this.cfg);
				editenterprsie_EnterpriseEuthentication_formpanel_win.superclass.constructor
						.call(this, {
							width : 500,
							title : '修改<font color="red">'
									+ this.cfg.declare_year
									+ '年'
									+ getDataType(this.cfg.authentication_type_sb)
									+ '</font>质量体系环境认证数据',
							iconCls : 'configmanage',
							// layout : 'form',
							closeAction : 'hide',
							frame : true,
							modal : true,
							plain : true,
							items : this.panel,
							buttons : [{
										text : '保存修改草稿',
										scope : this,
										iconCls : 'itemadd',
										handler : this.submit
									}, {
										text : '取消',
										scope : this,
										iconCls : 'cancel',
										handler : function() {
											this.hide()
										}
									}]
						});
				this.addEvents('editok');
			},
			submit : function() {
				var form = this.panel.getForm();
				if (form.isValid()) {
					var eaid = form.findField('eaid').getValue();
					var eid = form.findField('eid').getValue();
					var authentication_type = form.findField('authentication_type').getValue();
					var authentication_name = form.findField('authentication_name').getValue();
					var authentication_department = form.findField('authentication_department').getValue();
					var authentication_time = form.findField('authentication_time').getValue();
					Ext.Ajax.request({
				url : 'cxxqysjsb/quantity_environment/updateEnterpriseAuthentication.action?',
				params : {
					eaid : eaid,
					eid : eid,
					authentication_type : authentication_type,
					authentication_name : authentication_name,
					authentication_department : authentication_department,
					authentication_time : authentication_time
				},
				scope : this,
				success : function(response, options) {
					if (response.responseText == 'ok') {
						this.fireEvent('editok');
						this.hide();
					} else if (response.responseText == 'fail') {
						Ext.Msg.show({
									title : '提示',
									msg : '数据传输失败',
									icon : Ext.Msg.WARNING,
									buttons : Ext.Msg.OK
								});
						return;
					}
				}
			});
				}
			}
		});
Ext.extend(demo.module, {
	init : function() {
		this.enterprise_authentication = new enterprsie_EnterpriseEuthentication_grid();
		this.main.add(this.enterprise_authentication);
		this.main.doLayout();
	}
});
